package cn.vin;

import cn.vin.dao.knife.FeiDao;
import cn.vin.dao.user.PeopleDao;
import cn.vin.service.PeopleService;
import com.kariqu.log.*;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;

@SpringBootApplication(scanBasePackages={"com.kariqu","cn.vin"})
@RestController
@EnableAdminServer
@EnableScheduling
public class Application {
private Logger logger = LoggerFactory.getLogger(Application.class);
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class);
        app.setAdditionalProfiles("dev");
        app.run(args);
    }
    @Resource
    private PeopleService peopleService;

    @GetMapping("/fun")
    private void fun() {
        peopleService.test();
    }

    @Resource
    private BinLogUtil binLogUtil;

    @GetMapping("/demo1")
    public String demo1(Integer num) throws IOException {
//        logger.error("test",new RuntimeException());
        for(int i=0; i<num; i++) {
            FdjfdLog binLog = new FdjfdLog();
            binLog.setOpenid("test");
            binLog.setUserId(1L);
            binLog.setBaseType(RandomUtils.nextInt(1,4));
            binLog.setSubType(RandomUtils.nextInt(1,10));
            binLog.setExtra("afsf");
            binLogUtil.log(binLog);
        }
        for(int i=0; i<num; i++) {
            QqsysLog binLog = new QqsysLog();
            binLog.setOpenid("test");
            binLog.setUserId(1L);
            binLog.setBaseType(RandomUtils.nextInt(1,4));
            binLog.setSubType(RandomUtils.nextInt(1,10));
            binLog.setExtra("afsf");
            binLogUtil.log(binLog);
        }
        for(int i=0; i<num; i++) {
            BinLog binLog = new StickLog();
            binLog.setOpenid("test");
            binLog.setUserId(1L);
            binLog.setBaseType(RandomUtils.nextInt(1,4));
            binLog.setSubType(RandomUtils.nextInt(1,10));
            binLog.setExtra("afsf");
            binLogUtil.log(binLog);
        }
        for(int i=0; i<num; i++) {
            BinLog binLog = new QqdjLog();
            binLog.setOpenid("test");
            binLog.setUserId(1L);
            binLog.setBaseType(RandomUtils.nextInt(1,4));
            binLog.setSubType(RandomUtils.nextInt(1,10));
            binLog.setExtra("afsf");
            binLogUtil.log(binLog);
        }
        return "ok";
    }
    @GetMapping("/demo2")
    public String demo2() throws InterruptedException {
        Thread.sleep(1000);
        logger.info("============demo2---");
        return "demo2";
    }

    /**
     * 跨域
     * @return
     */
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*")
                        .allowedHeaders("*")
                        .allowedMethods("*");
            }
        };
    }

    /*@Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        // 目的是
        return args -> {
            System.out.println("来看看 SpringBoot 默认为我们提供的 Bean：");
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
//            Arrays.stream(beanNames).forEach(System.out::println);
        };
    }*/
   /* @Autowired
    private GracefulShutdownTomcat gracefulShutdownTomcat;

    @Bean
    public ServletWebServerFactory servletContainer(TomcatServletWebServerFactory tomcatFactory) {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addConnectorCustomizers(gracefulShutdownTomcat);
        return tomcat;
    }*/
}
